set(CMAKE_CXX_FLAGS "-W -Wall -Wextra -Wno-unused-parameter -O3 ${CMAKE_CXX_FLAGS}")

add_executable(tv tv.h tv.cpp)
target_sources(tv PRIVATE
  # Source seg
  support_source/image_source.h
  support_source/image_source.cpp

  # Canvas seg
  support_canvas/iterm2_canvas.h
  support_canvas/iterm2_canvas.cpp
  support_canvas/kitty_canvas.h
  support_canvas/kitty_canvas.cpp
  support_canvas/terminal_canvas.h
  support_canvas/terminal_canvas.cpp
  support_canvas/unicode_block_canvas.h
  support_canvas/unicode_block_canvas.cpp

  # Renderer seg
  renderer/renderer.h
  renderer/renderer.cpp

  # Helper seg
  helper/buffered_write_sequencer.h
  helper/buffered_write_sequencer.cpp
  helper/display_options.h
  helper/framebuffer.h
  helper/framebuffer.cpp
  helper/utils.h
  helper/utils.cpp
  helper/term_query.h
  helper/term_query.cpp
  helper/thread_pool.h
  helper/tv_base64.h
  helper/tv_png.h
  helper/tv_png.cpp
  helper/tv_time.h
  helper/tv_help.h
  helper/tv_help.cpp
)

target_link_libraries(tv Threads::Threads)


set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
if (LIBDEFLATE_PKGCONFIG_FOUND)
  target_link_libraries(tv PkgConfig::LIBDEFLATE_PKGCONFIG)
else()
  # Use fallback.
  target_link_libraries(tv ${LIBDEFLATE_LIBRARY})
endif()

target_include_directories(tv PRIVATE ${CMAKE_BINARY_DIR}/src)
target_include_directories(tv PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} helper inc_data renderer support_canvas support_source)

target_compile_features(tv PRIVATE cxx_std_17)

if(TV_VERSION_FROM_GIT)
  git_describe(GIT_DESCRIBE_VERSION)
  git_committime(GIT_DATE)
endif()
if(NOT GIT_DESCRIBE_VERSION OR NOT TV_VERSION_FROM_GIT)
  unset(GIT_DATE)
  set(GIT_DESCRIBE_VERSION "${PROJECT_VERSION}+")
endif()

configure_file(tv_version.h.in tv_version.h)


if(WITH_LIBSIXEL)
  target_sources(tv PUBLIC
  support_canvas/sixel_canvas.h
  support_canvas/sixel_canvas.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_SIXEL)
  target_link_libraries(tv PkgConfig::LIBSIXEL)
endif()

if(WITH_GRAPHICSMAGICK)
  target_sources(tv PUBLIC
  support_source/graphics_magick_source.h
  support_source/graphics_magick_source.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_GRPAPHICSMAGICK)
  target_link_libraries(tv PkgConfig::GRAPHICSMAGICKXX)
endif()

if(WITH_RSVG)
  target_sources(tv PUBLIC
  support_source/svg_image_source.h
  support_source/svg_image_source.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_RSVG)
  target_link_libraries(tv PkgConfig::RSVG PkgConfig::CAIRO)
endif()

if(WITH_POPPLER)
  target_sources(tv PUBLIC
  support_source/pdf_image_source.h
  support_source/pdf_image_source.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_POPPLER)
  target_link_libraries(tv PkgConfig::POPPLER PkgConfig::CAIRO)
endif()

if(WITH_TURBOJPEG)
  target_sources(tv PUBLIC
  support_source/jpeg_source.h
  support_source/jpeg_source.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_JPEG)
  target_link_libraries(tv
    PkgConfig::TURBOJPEG
    PkgConfig::EXIF
    PkgConfig::AVUTIL
    PkgConfig::SWSCALE)
endif()

if(WITH_OPENSLIDE_SUPPORT)
  target_sources(tv PUBLIC
  support_source/openslide_source.h
  support_source/openslide_source.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_OPENSLIDE_SUPPORT)
  target_link_libraries(tv PkgConfig::OPENSLIDE
    PkgConfig::AVUTIL
    PkgConfig::SWSCALE)
endif()

if(WITH_VIDEO_DECODING)
  target_sources(tv PUBLIC
  support_source/video_source.h
  support_source/video_source.cpp)
  target_compile_definitions(tv PUBLIC WITH_TV_VIDEO)
  target_link_libraries(tv
    PkgConfig::LIBAV
    PkgConfig::AVUTIL
    PkgConfig::SWSCALE)
  if (WITH_VIDEO_DEVICE)
    target_link_libraries(tv PkgConfig::LIBAV_DEVICE)
    add_definitions(-DHAVE_AVDEVICE)
  endif()
endif()

if(WITH_STB_IMAGE)
  # STB usuall does not come with pkg-config. Just check if we see the include
  include(CheckIncludeFileCXX)
  CHECK_INCLUDE_FILE_CXX(stb/stb_image.h HAVE_STB)
  target_sources(tv PUBLIC
  support_source/stb_image_source.h
  support_source/stb_image_source.cpp)
  if(NOT HAVE_STB)
    message("--> Using STB from third_party/ instead")
    target_include_directories(tv PRIVATE ${CMAKE_SOURCE_DIR}/third_party)
  endif()
  CHECK_INCLUDE_FILE_CXX(stb/stb_image_resize2.h STB_RESIZE_VERSION2)
  if(STB_RESIZE_VERSION2)
    target_compile_definitions(tv PUBLIC STB_RESIZE_VERSION2)
  endif()
  target_compile_definitions(tv PUBLIC WITH_TV_STB)
endif()

if(WITH_QOI_IMAGE)
  include(CheckIncludeFileCXX)
  CHECK_INCLUDE_FILE_CXX(qoi.h HAVE_QOI)
  target_sources(tv PUBLIC
  support_source/qoi_image_source.h
  support_source/qoi_image_source.cpp)
  if(NOT HAVE_QOI)
    message("--> Using QOI from third_party/ instead")
    target_include_directories(tv PRIVATE ${CMAKE_SOURCE_DIR}/third_party/qoi)
  endif()
  target_compile_definitions(tv PUBLIC WITH_TV_QOI)
  target_link_libraries(tv
    PkgConfig::AVUTIL
    PkgConfig::SWSCALE)
endif()

# We always take the manpage from the checkout currently so that we don't
# require pandoc to build.

############ MAYBE NOT WORK !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# message("NOTE: ++++++++++++++++++++++++++++++++++++++++++++="${CMAKE_CURRENT_BINARY_DIR})
# message("NOTE: ++++++++++++++++++++++++++++++++++++++++++++="${CMAKE_CURRENT_SOURCE_DIR})
# add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/inc_data/tv_manpage.inc
#   COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/create-manpage-inc.sh" ${CMAKE_CURRENT_SOURCE_DIR}/../docs/tv.1 ${CMAKE_CURRENT_BINARY_DIR}/inc_data/tv_manpage.inc
#     DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../docs/tv.1
#     WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
#     COMMENT "Generating builtin manpage")
# add_custom_target(builtin-manpage DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/inc_data/tv_manpage.inc)

# Currently disabled. The ancient `man` command on MacOS is misbehaving, so
# for now, it is checked into the repo at src/tv_manpage.inc
#add_dependencies(tv builtin-manpage)


install(TARGETS tv RUNTIME DESTINATION bin)
